﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Antu.Business.Business;
using Antu.Business;
using Antu.Business.AbstractClass;

namespace DataTranslation
{
    public partial class FrmDataSelect : Form
    {
        private AbstractDataOperation clsDataTranlation;

        public FrmDataSelect()
        {
            InitializeComponent();

            clsDataTranlation = DataTranslationFactory.CreateDataOperation(
                GlobalSetting.SystemTranslationType);
        }

        /// <summary>
        /// 取消关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 自定义函数
        /// <summary>
        /// 获取选择的数据表
        /// </summary>
        /// <returns></returns>
        private List<string> GetSelectedTables()
        {
            List<string> lstTables = new List<string>();

            foreach (object objValue in lstTargetTables.Items)
            {
                string strValue = objValue.ToString();
                if (!lstTables.Contains(strValue))
                {
                    lstTables.Add(strValue);
                }
            }

            return lstTables;
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BingData()
        {
            List<string> lstSource = ClsOracleSystemOperation.GetAllTablesName();

            lstSourceTables.Items.Clear();
            foreach (string strTable in lstSource)
            {
                lstSourceTables.Items.Add(strTable);
            }
        } 
        #endregion

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmDataSelect_Load(object sender, EventArgs e)
        {
            BingData();
        }

        #region 选择表的事件

        private void btnAllSelect_Click(object sender, EventArgs e)
        {
            foreach (object objValue in lstSourceTables.Items)
            {
                if (!lstTargetTables.Items.Contains(objValue))
                {
                    lstTargetTables.Items.Add(objValue);
                }
            }
        }

        private void btnSelectOne_Click(object sender, EventArgs e)
        {
            object objSelect = lstSourceTables.SelectedItem;

            if (objSelect != null)
            {
                if (!lstTargetTables.Items.Contains(objSelect))
                {
                    lstTargetTables.Items.Add(objSelect);
                }
            }
        }

        private void btnUnselectOne_Click(object sender, EventArgs e)
        {
            int nIndex = lstTargetTables.SelectedIndex;
            if (nIndex != -1)
            {
                lstTargetTables.Items.RemoveAt(nIndex);
            }
        }

        private void btnAllUnselect_Click(object sender, EventArgs e)
        {
            lstTargetTables.Items.Clear();
        }

        private void lstSourceTables_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            btnSelectOne_Click(null, null);
        }

        private void lstTargetTables_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            btnUnselectOne_Click(null, null);
        } 

        #endregion

        #region 导出动作
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            if (lstTargetTables.Items.Count > 0)
            {
                Application.DoEvents();
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    //获取选择表的数据
                    List<string> lstTables = GetSelectedTables();
                    DataSet dsGet = ClsOracleSystemOperation.GetNeedExportData(lstTables);

                    if (GlobalSetting.TempDatabaseFileType == TempDataType.Xml)
                    {
                        SaveFileDialog ofdDialog = new SaveFileDialog();
                        ofdDialog.InitialDirectory = Application.StartupPath;
                        ofdDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
                        if (ofdDialog.ShowDialog() == DialogResult.OK)
                        {
                            //获取文件名
                            string strFileName = ofdDialog.FileName;

                            //导出xml
                            dsGet.WriteXml(strFileName);
                        }
                    }
                    else if (GlobalSetting.TempDatabaseFileType == TempDataType.Mdb)
                    {
                        //导出xml
                        clsDataTranlation.ExportData(dsGet);
                    }

                    MessageBox.Show("导出成功");
                }
                catch (Exception ex)
                {
                    ClsMsgBoxAppear.MsgBoxError(ex);
                }
                finally
                {
                    Application.DoEvents();
                    this.Cursor = Cursors.Default;
                }
            }
        } 
        #endregion
    }
}